草庐IT

c++ - Clang、std::next、libstdc++ 和 constexpr-ness

全部标签

c++ - 是否可以使用填充构造函数创建 std::vector<std::unique_ptr<Bar>> ?

我有一个Foo类,其成员变量类型为std::vector>,我想填写这个类的构造函数的初始化列表。这可能吗?我希望可以使用vector的填充构造函数,就像这样Foo::Foo(intn):vector>(n,unique_ptr(newBar)){}但我认为这需要std::unique_ptr的复制构造函数,它被删除了(因为它应该被删除)(unique_ptr(constunique_ptr&)=delete)。有没有更好的方法来解决这个问题? 最佳答案 既然不可复制,那就搬吧!硬编码对象的解决方案:#include#include

c++ - 如何修复 C++11 中 std::chrono 比较的编译错误?

我正在按照示例ASIOserverwithtimeout,此处显示的函数行已从deadline_timer::traits_type::now()修改为std::chrono::steady_clock::now()因为我想使用不带boost的独立ASIO。ASIO可以独立使用C++11。voidcheck_deadline(deadline_timer*deadline){if(stopped())return;//Checkwhetherthedeadlinehaspassed.comparethedeadlineagainst//thecurrenttime//Imodified

c++ - std::sort 如何修改支持类型?

当我使用begin()和在double的vector上调用sort时end()迭代器sort函数如何修改原始vector以包含排序后的值?虽然迭代器只是表示一个值,但它们怎么会导致原始vector被修改?vectornums={10.33,20.44,60.77};sort(nums.begin(),nums.end();//howdoestheoriginalnumsgetchanged? 最佳答案 迭代器不代表值,它代表容器、流或流缓冲区中的某个位置。本质上,它们是指针的概括。一些迭代器允许您使用间接(*it)修改它们迭代的内容

c++ - 是否有可能在 C++ 中获取 std::array 的子数组?

我想做这样的事std::arrayarray1={{...}};conststd::array&array2=array1[1:4];//[x:y]doesn'texist也就是说,获取一个数组,该数组是另一个数组的一种View,而无需复制它。 最佳答案 不,你不能那样做。所有标准库容器都是其数据的唯一所有者,std::array也不异常(exception)。事实上,std::array被限制为以这样的方式实现,以便元素存储在类的实际数组成员中,这将不可能使用别名。有一个关于array_view类的提议,该类将表示对连续数据blo

c++ - std::list 的多元素插入是强异常安全的吗?

在exceptionalc++的第17项中,我发现:First,forallcontainers,multi-elementinserts("iteratorrange"inserts)areneverstronglyexception-safe.但在effectiveSTL的第1项中,我发现:Ifyouneedtransactionalsemanticsformultiple-elementinsertions(e.g.,therangeform—seeItem5),you'llwanttochooselist,becauselististheonlystandardcontaine

c++ - 两个使用不同 libstdc++ 版本的静态库

我正在开发一个使用旧版本libstdc++的项目。我刚刚介绍了一个静态链接的新库,它需要更新版本的libstdc++。我已经安装了两个版本的libstdc++。有没有办法用gcc和新库编译我的程序? 最佳答案 使用较新版本的libstdc++编译为thislibraryisforwardcompatible. 关于c++-两个使用不同libstdc++版本的静态库,我们在StackOverflow上找到一个类似的问题: https://stackoverflo

c++ - 特征矩阵初始化的 clang 格式

例如要初始化Eigen::Matrix3i我们可以使用语法:Eigen::Matrix3iT;T但是,当使用clang-format(在我的例子中是3.6)和Google样式时,这个漂亮的初始化变成:Eigen::Matrix3iT;T有没有简单的方法可以避免这种情况?有没有办法告诉clang-format跳过这样的东西? 最佳答案 看来您唯一的选择是使用相当丑陋的clang格式切换语法:Eigen::Matrix3iT;//clang-formatoffT 关于c++-特征矩阵初始化的

c++ - 无法获取要在 Visual Studio 和 clang 下编译的模板

我有以下缩小代码。带有//OnlyVS的行在VS上编译但在clang上不编译,带有//Onlyclang的行在clang上编译,但在VS上不编译。谁是正确的?更重要的是,如何在两者上编译等效行?测试的版本是clang3.7.0,VS2015。#include#includetemplateclassC{structB{std::functionfunc;B(std::functionfunc):func(func){}};templatestructD:B{usingB::B;templatevoidCall(T&t,std::index_sequence){func(std::get

c++ - 如何在 OpenMP 4 中为 std::vector 写入 "target data map"?

我想使用C++和vectors。我有C代码和这样创建的C数组:double*data=(double*)malloc(sizeof(double)*n);double*result=(double*)malloc(sizeof(double)*n);#pragmaomptargetdatamap(tofrom:data[0:n],result[0:n])//loop现在我使用C++vector,我得到:example.cpp:31:41:error:expectedvariablenameoranarrayitem#pragmaomptargetdatamap(tofrom:data[

c++ - Clang 和 GCC 在解析可变函数模板重载时的不同行为

考虑这段代码:#includeintfoo_i(intx){returnx+1;}charfoo_c(charx){returnx+1;}usingII=int(*)(int);usingCC=char(*)(char);templatestructfn{Ff;templatedecltype(auto)operator()(Args&&...args)const{returnf(std::forward(args)...);}};structfn_2:privatefn,privatefn{fn_2(IIfp1,CCfp2):fn{fp1},fn{fp2}{}usingfn::ope